package io.renren.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description
 * @Author Administrator
 * @Date 2017/9/8
 */
public class SqlHelper {


    /**
     * 匹配任何非空白字符串的组合
     * 		str 	|	result
     * 	 -----------|--------------
     * 		abc123	|	[abc123]
     * 		abc abc |   [abc,abc]
     * 		abc\tabc|   [abc,abc]
     *
     * @author ZhangQingmu
     * @date   2017年2月9日
     * @param str
     * @return
     */
    public static List<String> splitBySpace(String str) {
        return splitByRegex(str, "\\S+");
    }
    public static String splitForStrBySpace(String str) {
        return splitForStrByRegex(str, "\\S+");
    }

    /**
     *
     * @author ZhangQingmu
     * @date   2017年2月9日
     * @param str
     * @param regex
     * @return
     */
    public static List<String> splitByRegex(String str, String regex) {
        List<String> res = new ArrayList<>();
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        while (m.find()) {
            res.add(m.group());
        }
        return res;
    }
    public static String splitForStrByRegex(String str, String regex) {
        StringBuffer sb=new StringBuffer();
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(str);
        while (m.find()) {
            sb.append("'"+m.group()+"',");
        }
        return null==sb||sb.length()<=0?"":sb.toString().substring(0,sb.length()-1);
    }

    /**
     * 转换成支持空格分割的查询条件
     * @param conds
     * @param origin
     * @param dest
     */
    public static void convertSplitCond(Map<String, Object> conds, String origin, String dest) {

        if(conds.containsKey(origin)) {
            List<String> listdata = SqlHelper.splitBySpace(String.valueOf(conds.get(origin)));
            if (listdata != null && listdata.size() > 1) {
                conds.remove(origin);
                conds.put(dest, listdata);
            }
        }

    }


    /**
     * 转换成支持空格分割的查询条件
     * @param conds
     * @param origin
     * @param dest
     */
    public static void convertSplitCondForInvoice(Map<String, Object> conds, String origin, String dest) {

        if(conds.containsKey(origin)) {
            List<String> listdata = SqlHelper.splitBySpace(String.valueOf(conds.get(origin)));
            if (listdata != null && listdata.size() >= 1) {
                conds.remove(origin);
                conds.put(dest, listdata);
            }
        }

    }


    /**
     * 转换成支持空格分割的查询条件
     * @param conds
     * @param origin
     * @param dest
     */
    public static void convertSplitCond2(Map<String, Object> conds, String origin, String dest) {

        if(conds.containsKey(origin)) {
            String str= SqlHelper.splitForStrBySpace(String.valueOf(conds.get(origin)));
            if (str != null && !"".equals(str)) {
                conds.remove(origin);
                conds.put(dest,str);
            }
        }

    }

    /**
     * 转换成支持空格分割的查询条件 转换后调教 等于原始条件 加 '_in'
     * @param conds
     * @param origin
     */
    public static void convertSplitCond(Map<String, Object> conds, String origin) {

        convertSplitCond(conds, origin, origin + "_in");

    }
}
